﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Medicalsystem.MCP.FormDesigner.Common;

namespace Medicalsystem.MCP.FormDesigner
{
    /// <summary>
    /// 容器基类
    /// </summary>
    public abstract class ContainerHandler
    {
        public ContainerHandler(System.Windows.Forms.Control container, CtrlMngDesign parentctrlMngDesign, ModelDesign modelDesign)
        {
            _container = container;
            _parentctrlMngDesign = parentctrlMngDesign;
            _modelDesign = modelDesign;

            _container.Paint += new PaintEventHandler(Container_Paint);
            _UISKnob = new UISizeKnob(this);
            _controls = new List<CtrlMngDesign>();
        }

        private System.Windows.Forms.Control _container;
        private ModelDesign _modelDesign;
        private CtrlMngDesign _parentctrlMngDesign;
        private UISizeKnob _UISKnob;
        private List<CtrlMngDesign> _controls;

        /// <summary>
        /// 移动区域
        /// </summary>
        protected Rectangle _mouseRect = Rectangle.Empty;

        #region 属性

        /// <summary>
        /// 控件容器
        /// </summary>
        public Control Container
        {
            get { return _container; }
            set { _container = value; }
        }

        /// <summary>
        /// 容器子控件
        /// </summary>
        public List<CtrlMngDesign> Controls
        {
            get { return _controls; }
        }

        /// <summary>
        /// 父级控件管理者
        /// </summary>
        public CtrlMngDesign ParentControlMngDesign
        {
            get { return _parentctrlMngDesign; }
            set { _parentctrlMngDesign = value; }
        }

        /// <summary>
        /// 属于的模板管理者
        /// </summary>
        public ModelDesign ModelDesign
        {
            get { return _modelDesign; }
            set { _modelDesign = value; }
        }

        #endregion 属性

        /// <summary>
        /// 控件添加到容器-同时添加到模板主列表
        /// </summary>
        /// <param name="ctrl"></param>
        public void Add(CtrlMngDesign ctrl)
        {
            this._controls.Add(ctrl);
            this._modelDesign.Controls.Ctrls.Add(ctrl);
            this.Container.Controls.Add(ctrl.Owner);

            //ctrl.Owner.Visible = true;
        }

        /// <summary>
        /// 设置控件捏手
        /// </summary>
        /// <param name="ctrlMD">焦点控件(null 不显示捏手)</param>
        public void SetControlKnob(CtrlMngDesign ctrlMD)
        {
            if (ctrlMD == null)
            {
                this._UISKnob.ShowUISizeDots(null, false, false);
            }
            else
            {
                this._UISKnob.ShowUISizeDots(ctrlMD.Owner, true, true);
            }
        }

        public void KnobBringToFront()
        {
            this._UISKnob.BringToFront();
        }

        /// <summary>
        /// 重置为初始状态
        /// </summary>
        public void Reset()
        {
            this._controls.Clear();
            this._container.Controls.Clear();
            this._UISKnob = new UISizeKnob(this);
        }

        #region 选取框方法

        /// <summary>
        /// 矩形选区
        /// </summary>
        /// <param name="p"></param>
        /// <param name="fs"></param>
        protected void ResizeToRectangle(Point p, FrameStyle fs)
        {
            DrawRectangle(fs);
            _mouseRect.Width = p.X - _mouseRect.Left;
            _mouseRect.Height = p.Y - _mouseRect.Top;
            DrawRectangle(fs);
        }

        private void DrawRectangle(FrameStyle fs)
        {
            Rectangle rect = this._container.RectangleToScreen(_mouseRect);
            ControlPaint.DrawReversibleFrame(rect, Color.Transparent, fs);
        }

        #endregion 选取框方法

        #region 绘制选区控件虚线框

        /// <summary>
        /// 绘制容器中选择控件的虚线框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Container_Paint(object sender, PaintEventArgs e)
        {
            try
            {
                if (!this._modelDesign.BeginMove && _modelDesign.CtrlMngDesignFocused != null && _modelDesign.CtrlMngDesignFocused.ParentContainer.Container == _container)
                {
                    foreach (CtrlMngDesign ctrlMngDesign in _modelDesign.SelectList)
                    {
                        System.Drawing.Pen P = new System.Drawing.Pen(System.Drawing.Color.DimGray);
                        P.Color = Color.Red;
                        P.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                        e.Graphics.DrawRectangle(P, new System.Drawing.Rectangle(new System.Drawing.Point(ctrlMngDesign.Owner.Location.X - 2, ctrlMngDesign.Owner.Location.Y - 2), new System.Drawing.Size(ctrlMngDesign.Owner.Width + 3, ctrlMngDesign.Owner.Height + 3)));
                    }
                }
            }
            catch (Exception ex)
            {
                TryCustomException.TransactException(ex);
            }
        }

        #endregion 绘制选区控件虚线框
    }
}